package com.futures.questionbank.main;

import android.Manifest;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.SPUtil;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.SQLdm;
import com.futures.questionbank.db.table.User;
import com.futures.questionbank.db.table.User_Table;
import com.futures.questionbank.view.CustomLoadingDialog;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/6/15 0015 16:13
 * version: 1.0
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.sb_login)
    SuperButton sbLogin;
    @BindView(R.id.tv_registered)
    TextView tvRegistered;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.login_activity);
        setTitle("登录");
    }

    @Override
    protected void loadData() {
        RxPermissions rxPermissions = new RxPermissions(LoginActivity.this);
        rxPermissions
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                        //打开数据库输出流
                        SQLdm s = new SQLdm();
                        SQLiteDatabase db = s.openDatabase(getApplicationContext());
                        User userModel2 = new User();
                        userModel2.setPhone("15007201559");
                        userModel2.setPassword("123456");
                        userModel2.save();
                        //  Toast.makeText(MainActivity.this, "同意权限", Toast.LENGTH_SHORT).show();
                    } else {
                        //  Toast.makeText(MainActivity.this, "拒绝权限", Toast.LENGTH_SHORT).show();
                    }
                });
      //  FlowManager.getModelAdapter(User.class).insert(userModel);
    }



    @OnClick({R.id.sb_login, R.id.tv_registered, R.id.tv_forget_password})
    public void onViewClicked(View view) {
        if (!UItils.isNetWorkConnected()) {
            UItils.showToastSafe("网络已断开,请检查网络连接");
            return;
        }
        switch (view.getId()) {
            case R.id.sb_login:
                UItils.showToastSafe("登录成功");
                UItils.startActivity(MainActivity2.class);
               // SPUtil.put("phone",user.getPhone());
                finish();
                if (!UItils.isPhoneNumber(etPhone.getText().toString().trim())) {
                    UItils.showToastSafe("请输入正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
                    UItils.showToastSafe(etPassword.getHint().toString());
                    return;
                }
                CustomLoadingDialog.show(this);
                //根据 name 单个查询
                User user = new Select()
                        .from(User.class)
                        .where(User_Table.phone.is(etPhone.getText().toString().trim()))
                        .querySingle();
                if (null != user){
                    if (user.getPhone().equals(etPhone.getText().toString().trim()) && user.getPassword().equals(etPassword.getText().toString().trim())){
                        final Timer t = new Timer();
                        t.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                UItils.showToastSafe("登录成功");
                                UItils.startActivity(MainActivity2.class);
                                SPUtil.put("phone",user.getPhone());
                                finish();
                                CustomLoadingDialog.dismiss(LoginActivity.this);
                                t.cancel();
                            }
                        }, 2000);
                    }else {
                        UItils.showToastSafe("帐号输入有误或密码不正确");
                        CustomLoadingDialog.dismiss(this);
                    }

                }else {
                    UItils.showToastSafe("帐号未注册");
                    CustomLoadingDialog.dismiss(this);
                    return;
                }

                break;
            case R.id.tv_registered:
                UItils.startActivity(RegisterActivity.class);
                break;
            case R.id.tv_forget_password:
                UItils.startActivity(ForgetPasswordActivity.class);
                break;
                default:
        }
    }
}
